# 抓取优美图库
# 1. 拿到主页的源代码，然后提取到子页面的链接地址href
# 2. 通过 href 拿到子页面的内容。从子页面中找到图片的下载地址 img -> src
# 3. 下载图片

import requests
from bs4 import BeautifulSoup

domain = 'https://www.umei.cc'
resp = requests.get(domain + '/bizhitupian/weimeibizhi/')
resp.encoding = 'utf-8'  # 指定字符集
# print(resp.text)
resp.close()

# 将源代码交给 bs4
main_page = BeautifulSoup(resp.text, "html.parser")
aList = main_page.find('div', class_='item_list infinite_scroll').find_all('a')
# print(aList)
for a in aList:
    child_href = domain + a.get('href')  # 直接通过get就可以拿到属性值
    # 拿到子页面的源代码
    child_page_href = requests.get(child_href)
    child_page_href.encoding = 'utf-8'
    child_page_text = child_page_href.text
    # 从子页面中拿到图片的下载路径
    child_page = BeautifulSoup(child_page_text, 'html.parser')
    img = child_page.find('div', class_="big-pic").find('img')
    img_src = img.get('src')

    # 下载图片
    img_resp = requests.get(img_src)
    # img_resp.content # 这里拿到的是字节
    with open("爬虫文件/" + img_src.split('/')[-1], mode='wb') as file:
        file.write(img_resp.content)  # 将图片内容写入到文件
    file.close()

    break  # 测试使用
